<?php

namespace App\Http\Controllers;

use App\Models\ActivityType;
use Illuminate\Http\Request;
use Carbon\Carbon;

class ActivityTypeController extends Controller
{
    /**
     * 获取活动类型列表
     */
    public function index(Request $request)
    {
        try {
            $query = ActivityType::query();
            
            // 搜索条件
            if ($request->has('name')) {
                $query->where('name', 'like', '%' . $request->name . '%');
            }
            
            if ($request->has('status')) {
                $query->where('status', $request->status);
            }
            
            // 排序
            $query->orderBy('sort_order', 'asc')
                  ->orderBy('id', 'desc');
            
            $types = $query->get();
            
            return response()->json([
                'code' => 200,
                'message' => '获取成功',
                'data' => $types
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'code' => 500,
                'message' => '获取失败',
                'error' => $e->getMessage()
            ], 500);
        }
    }

    /**
     * 创建活动类型
     */
    public function store(Request $request)
    {
        try {
            $request->validate([
                'name' => 'required|string|max:50|unique:activity_type,name',
                'status' => 'required|integer|in:0,1',
                'sort_order' => 'integer'
            ]);

            $type = ActivityType::create([
                'name' => $request->name,
                'status' => $request->status,
                'sort_order' => $request->sort_order ?? 99,
                'create_time' => Carbon::now(),
                'update_time' => Carbon::now()
            ]);

            return response()->json([
                'code' => 200,
                'message' => '创建成功',
                'data' => $type
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'code' => 500,
                'message' => '创建失败',
                'error' => $e->getMessage()
            ], 500);
        }
    }

    /**
     * 获取单个活动类型
     */
    public function show($id)
    {
        try {
            $type = ActivityType::find($id);
            
            if (!$type) {
                return response()->json([
                    'code' => 404,
                    'message' => '活动类型不存在'
                ], 404);
            }

            return response()->json([
                'code' => 200,
                'message' => '获取成功',
                'data' => $type
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'code' => 500,
                'message' => '获取失败',
                'error' => $e->getMessage()
            ], 500);
        }
    }

    /**
     * 更新活动类型
     */
    public function update(Request $request, $id)
    {
        try {
            $type = ActivityType::find($id);
            
            if (!$type) {
                return response()->json([
                    'code' => 404,
                    'message' => '活动类型不存在'
                ], 404);
            }

            $request->validate([
                'name' => 'required|string|max:50|unique:activity_type,name,' . $id,
                'status' => 'required|integer|in:0,1',
                'sort_order' => 'integer'
            ]);

            $type->update([
                'name' => $request->name,
                'status' => $request->status,
                'sort_order' => $request->sort_order ?? $type->sort_order,
                'update_time' => Carbon::now()
            ]);

            return response()->json([
                'code' => 200,
                'message' => '更新成功',
                'data' => $type
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'code' => 500,
                'message' => '更新失败',
                'error' => $e->getMessage()
            ], 500);
        }
    }

    /**
     * 删除活动类型
     */
    public function destroy($id)
    {
        try {
            $type = ActivityType::find($id);
            
            if (!$type) {
                return response()->json([
                    'code' => 404,
                    'message' => '活动类型不存在'
                ], 404);
            }

            $type->delete();

            return response()->json([
                'code' => 200,
                'message' => '删除成功'
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'code' => 500,
                'message' => '删除失败',
                'error' => $e->getMessage()
            ], 500);
        }
    }
} 